package com.gree.scmservice.service.saas.impl;

import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.TablePageData;
import com.gree.common.utils.MinIoUtil;
import com.gree.scmservice.dao.saasDao.SaasCommodityDao;
import com.gree.scmservice.entity.saas.SaasCommodity;
import com.gree.scmservice.entity.saas.vo.SaasCommodityVo;
import com.gree.scmservice.service.saas.SaasCommdityService;
import com.gree.scmservice.util.UUIDUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

/**
 * 包路径: com.gree.scmservice.service.saan.impl
 * 项目名: scm-api
 * 创建时间: 2021-07-12 15:12
 * 描述: TODO
 **/
@Service
public class SaasCommdityServiceImpl implements SaasCommdityService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SaasCommodityDao saasCommodityDao;

    @Override
    public int saveCommodity(SaasCommodityVo saasCommodityVO) {
        saasCommodityVO.setCreate_time(LocalDateTime.now()); //创建时间
        saasCommodityVO.setGroup_id(UUIDUtil.getUUID().trim()); //商品Id
        SaasCommodity saasCommodity = new SaasCommodity(saasCommodityVO);
        SaasCommodity insert = saasCommodityDao.insertSaasCommodityDao(saasCommodity);
        return insert != null?1:0;
    }

    @Override
    public int updateCommodity(SaasCommodityVo saasCommodityVO) {
        saasCommodityVO.setModify_time(LocalDateTime.now());//更新时间
        SaasCommodity saasCommodity = new SaasCommodity(saasCommodityVO);
        return saasCommodityDao.updateSaasCommodityDao(saasCommodity);
    }

    @Override
    public SaasCommodity getByIdCommodity(String groupId) {
        SaasCommodity groupIdCommodity = saasCommodityDao.getGroupIdCommodity(groupId);
        return groupIdCommodity;
    }

    @Override
    public TablePageData<SaasCommodity> findAllPageCommodity(SaasCommodity saasCommodity) {
        TablePageData<SaasCommodity> tablePageData = saasCommodityDao.selectCommodityPage(saasCommodity);
        return tablePageData;
    }

    @Override
    public TablePageData<SaasCommodity> findAllPageCommodityOnUser(SaasCommodity saasCommodity) {
        saasCommodity.setStatus(1);
        TablePageData<SaasCommodity> tablePageData = saasCommodityDao.selectCommodityPage(saasCommodity);
        return tablePageData;
    }

    @Override
    public int deleteCommodity(String[] ids) {
        return saasCommodityDao.delCommodityIds(ids);
    }

    @Override
    public ReturnVo uploadIcon(MultipartFile file, String groupId) {
        String upload = MinIoUtil.upload("saas/commodity", "gscm", file);
        if(upload!=null){
            saasCommodityDao.updateSaasCommodityImage(groupId, upload);
            return ReturnVo.success(upload);
        }
        return ReturnVo.error();
    }

    @Override
    public void downloadImage(String image, HttpServletResponse response) {
        MinIoUtil.download("gscm", image, response);
    }
}
